#############################################################################################################################
#### Uncertainty, Cleavages and Ethnic Coalitions
### Replication Code for Figures 3 & 4
### Nils-Christian Bormann
### Journal of Politics

####################################################################################################
### Header
rm(list=ls())
setwd("") # set working directory

# install.packages(c("plyr", "reshape"), dependencies=TRUE)
library(plyr); library(reshape)
library(grid)

Brack <- function(x1,y1,x2,y2,h)
{
  x2 <- x2-x1; y2 <- y2-y1
  v1 <- viewport(x=x1,y=y1,width=sqrt(x2^2+y2^2),
                 height=h,angle=180*atan2(y2,x2)/pi,
                 just=c("left","bottom"),gp=gpar(col="black"))
  pushViewport(v1)
  grid.curve(x2=0,y2=0,x1=.125,y1=.5,curvature=.5)
  grid.move.to(.125,.5)
  grid.line.to(.375,.5)
  grid.curve(x1=.375,y1=.5,x2=.5,y2=1,curvature=.5)
  grid.curve(x2=1,y2=0,x1=.875,y1=.5,curvature=-.5)
  grid.move.to(.875,.5)
  grid.line.to(.625,.5)
  grid.curve(x2=.625,y2=.5,x1=.5,y1=1,curvature=.5)
  popViewport()
}

#############################################################################################################
#### Data
## requires executing formation script, lines 1-67 and 224-251
data <- read.csv("coalition_pp_by_regime.csv", header=T, sep=",")

## create factor
data$govtype <- NA
data$govtype[data$coalsize <= 0.5 & data$coalmemb == 1] <- 1 # single minority
data$govtype[data$coalsize > 0.5 & data$coalmemb == 1] <- 2 # single majority
data$govtype[data$coalsize <= 0.5 & data$coalmemb > 1] <- 3 # coalition minority
data$govtype[data$coalosize_flag==1] <- 5 # coalition majority
data$govtype[data$coalosize_flag==1 & data$coalsize==1] <- 6 # grand coalitions
data$govtype[is.na(data$govtype)] <- 4 # minimum-winning coalition

## non-western states
data$nonwest <- ifelse(data$cowid%in%c(2:368, 375, 740, 900,920), 0, 1)
table(data$nonwest)

################################################################################
### Figure 3: Distribution of government types
################################################################################

## prepare input
govnames <- c("Minority", "Majority", "Minority", "Min.-Win.", "Oversized", "Grand")

## plot
pdf("gov_type2-160909.pdf",  height=6, width=6*1.618)
  
  layout(matrix(c(1:8), 8, 1, byrow = TRUE), 
         widths=rep(1,8), heights=c(10,rep(1,7)))
  
  par(cex=1.1, las=1, cex.axis=1.1, cex.lab=1.1)
  par(mar=c(5,4,0,0)+.1) # inner margins
  par(omi=c(.5,0,0,0)) # outer margins
  
  #plot.default(x=c(0,7), y=c(0,.4), axes=F, type="n", xlab="", ylab="")
  plot.default(x=c(0,7), y=c(0,200), axes=F, type="n", xlab="", ylab="")
  
  axis(1, at=c(1.3,5), labels=c("Single-Member", "Coalitions"),
       outer=F,
       tick=F,
       line=2.2
  )
  
  axis(2,tick=F, las=1)
  
  abline(h=seq(0,.4,by=.05), lty=3, col="grey80")
  
  barplot(table(data$govtype[data$realcoal_flag==1]),
          ylab = "Count of Governments",
          col = "white", 
          add=T,
          border=T,
          axes=F,
          names.arg=govnames
  )
  
  barplot(table(data$govtype[data$realcoal_flag==1 & data$nonwest==1]),
          col = "grey50",
          add=T,
          border=NA,
          axes=F,
          names.arg=govnames
  )
  
  legend("topright", legend=c("West", "Non-West"), fill=c("white", "grey50"), bty="n")
  
  # curls underneath labels
  Brack(x1=0.98,x2=0.46,y1=.57,y2=0.57,h=0.03) 
  Brack(x1=0.41,x2=0.16,y1=.57,y2=0.57,h=0.03)
  
  # empty row
  par(mar=c(0,2,0,0)+.1, las=1, cex=1.1)
  plot(x=c(0,1), y=c(0,1), axes=F, type="n", xlab="", ylab="")
  
  for(i in 1:6){
    par(mar=c(0,10,0,0)+.1, las=1, cex=1.1)
    plot(x=c(0,1), y=c(0,1), axes=F, type="n", xlab="", ylab="")
    mtext(govnames[i], side=2, las=1, line=0.5, cex=1.25)
    box()
    abline(v=data$coalsize[data$govtype==i & data$realcoal_flag==1])
  }
  axis(1, tick=F, line=-.75)
  mtext("Included Population Share", side=1, line=1.4, cex=1.25)
  
  Brack(x1=0.12,x2=0.12,y1=.31,y2=0.37,h=0.03) 
  Brack(x1=0.12,x2=0.12,y1=.1,y2=0.225,h=0.03)
  
  mtext("Single-\n Member", side=2, line=6, at=6.3, cex=1.25)
  mtext("Coalitions", side=2, line=5.8, at=2.7, cex=1.25)
dev.off()


################################################################################
### Figure 4a: Government types under democracy and dictatorship (PACL, 2000)
################################################################################
## merge oversized and grand coalitions
data$govtype[data$govtype==6] <- 5

dddat <- melt(data[, c("cyear", "pp_dem", "pp_dic", "govtype")], id.vars=c("cyear", "govtype"))
names(dddat)[3:4] <- c("regime", "prob")

ddprobs <- ddply(dddat, .(cyear, govtype, regime), summarize, probsum=sum(prob))
ddprobs <- subset(ddprobs, !is.na(probsum))

################################################################################
## Boxplot
pdf("pp-ethgovtype_dd-170811.pdf", height=6, width=6*1.618)
	par(mar=c(5.1,4.1,1.1,1.1)) # w/o main title
  cex_factor <- 1.9 # 4 # for jpeg
	par(cex=cex_factor, las=1) 

	boxplot(probsum~regime*govtype, data=ddprobs, 
			col=(c("white","black")),
			medcol=c("black", "white"),
			main="Regime Type", 
			xlab="",
			boxwex=.5,
			axes=FALSE,
			xlim=c(1,14),
			at=(c(1,2,4,5,7,8,10,11,13,14)),
			cex=2,
			outline=F # no outliers
	)
	axis(1,at=c(1.5, 4.5, 7.5, 10.5, 13.5),
			c("Minority", "Majority", "Minority", "Min-Win", "Oversized"), tick=F)
	axis(1,at=c(1.5, 4.5), rep("",2), tick=T)
	axis(1,at=c(7.5, 10.5, 13.5), rep("",3), tick=T)
	mtext(c("Single-Member", "Coalition"),
			side=1, line=2.5, at=c(3,10.5), cex=cex_factor)
	axis(2,at=seq(0:.5, by=.2))
	mtext("Predicted Probability", las=0,
			side=2, line=3, cex=cex_factor)
		
	legend(x=6, y=.8,
			c("Democracy", "Dictatorship"),
			fill=c("white", "black"),
			bty="n",
			cex=1.1
		)
dev.off()


################################################################################
### Figure 4b: Government types under majoritarian & PR electoral rules
## from Bormann & Golder (2013)
################################################################################

esdat <- melt(data[, c("cyear", "pp_maj", "pp_pr", "govtype")], 
		id.vars=c("cyear", "govtype"))
names(esdat)[3:4] <- c("regime", "prob")

esprobs <- ddply(esdat, .(cyear, govtype, regime), summarize, probsum=sum(prob))
esprobs <- subset(esprobs, !is.na(probsum))
head(esprobs)

################################################################################
## Boxplot
pdf("pp-ethgovtype_es-170811.pdf", height=6, width=6*1.618)
	par(mar=c(5.1,4.1,1.1,1.1)) # w/o main title
	cex_factor <- 1.9 # 4 # for jpeg
	par(cex=cex_factor, las=1) 

	boxplot(probsum~regime*govtype, data=esprobs, 
			col=(c("white","black")),
			medcol=c("black", "white"),
			main="Electoral Systems", 
			xlab="",
			#ylab="Predicted Probability",
			boxwex=.5,
			axes=FALSE,
			xlim=c(1,14),
			at=(c(1,2,4,5,7,8,10,11,13,14)),
			cex=2,
			outline=F # no outliers
	)
	axis(1,at=c(1.5, 4.5, 7.5, 10.5, 13.5),
			c("Minority", "Majority", "Minority", "Min-Win", "Oversized"), tick=F)
	axis(1,at=c(1.5, 4.5), rep("",2), tick=T)
	axis(1,at=c(7.5, 10.5, 13.5), rep("",3), tick=T)
	mtext(c("Single-Member", "Coalition"),
			side=1, line=2.5, at=c(3,10.5), cex=cex_factor)
	axis(2,at=seq(0:.5, by=.2))

	legend(x=6, y=.9,
			c("Majoritarian", "PR"),
			fill=c("white", "black"),
			bty="n",
			cex=1.1
	)
dev.off()


